<?php
include "../base.php";
include "../server/ToolServer.php";
include "../server/HredisServer.php";
include "../server/SearchServer.php";

class index extends base
{
    public $toolServer;
    public $redisServer;
    public $now = 0;
    public $keyEvent = "JINGYUE_APP_EVENT"; //应用事件接收队列


    public function __construct()
    {
        $this->toolServer = new ToolServer();
        $this->redisServer = new HredisServer();
        $this->now = time();

    }

    public function exc()
    {
        $tool = $this->toolServer;
        try {
//            $rateList = [];
            $request = $_REQUEST;
            if (empty($request)) {
                $request = json_decode(file_get_contents("php://input"), true);
            }
            $hap = $request['hap'] ?? '';
            if (strpos($hap, 'huawei/') !== false) {
                $hap = str_replace('huawei/', 'huawei', $hap);
            }
            if (strpos($hap, 'huawei%2F') !== false) {
                $hap = str_replace('huawei%2F', 'huawei', $hap);
            }
            $hapArr = $this->toolServer->splitParam($hap);
            $call =
            $appid = $request['appid'] ?? '';
            if (empty($appid)) {
                $tool->error("invalid appid");
            }
            $producer = $request['producer'] ?? '';
            if (empty($producer)) {
                $tool->error("invalid producer");
            }
            $chanurlno = $hapArr['chanurlno'] ?? '';
            if (empty($chanurlno)) {
                $tool->error("invalid chanurlno");
            }
            $channo = $hapArr['channo'] ?? '';
            if (empty($channo)) {
                $tool->error("invalid channo");
            }
            $appEventField = ['getConfig_err', 'getConfig_success', 'getConfig', 'ADclick', 'ADshow', 'requestAD_err', 'requestAD_success', 'requestAD', 'stay_30', 'stay_20', 'stay_10', 'stay_5', 'stay_4', 'stay_3', 'stay_2', 'stay_1', 'stay_0'];
            $appEvent = $request['app_event'] ?? '';
            if (!in_array($appEvent, $appEventField)) {
                $tool->error("invalid app_event");
            }
            $adPlatform = $request['ad_platform'] ?? '';
            $platformEvent = $request['platform_event'] ?? '';
            $place_id = $request['place_id'] ?? '';
            if (!empty($platformEvent)) {
                if (empty($place_id)) {
                    $tool->error("invalid place_id");
                }
                if (empty($adPlatform)) {
                    $tool->error("invalid ad_platform");
                }
            }

            if (in_array($appEvent, ['ADclick', 'ADshow'])) {
                if (empty($place_id)) {
                    $tool->error("invalid place_id");
                }
                if (empty($adPlatform)) {
                    $tool->error("invalid ad_platform");
                }
//                if($adPlatform !== 'ads'){
//                    $rateList[$place_id][$appEvent] = (empty($rateList[$place_id][$appEvent])?0:$rateList[$place_id][$appEvent])+1;
//                }
            }
            $now = $this->now;
            $ymd = date("Ymd", $now);
            $eventRow = [
                'chanurlno' => $chanurlno,
                'channo' => $channo,
                'appid' => $appid,
                'producer' => $producer,
                'app_event' => $appEvent,
                'platform_event' => $platformEvent,
                'place_id' => $place_id,
                'ad_platform' => $adPlatform,
                'ts' => $now,
                'ymd' => $ymd,
                'create_at' => date('Y-m-d H:i:s', $now)
            ];
            $redisKey = $this->keyEvent;
            $this->redisServer->RPUSH($redisKey, json_encode($eventRow));
            $tool->success("success");
        } catch (Exception $e) {
            $tool->error('未知错误，稍后重试');
        }
    }


}

$wap = new index();
$wap->exc();
